package com.tx.activemq._01_c_p._02_topic;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.util.StringUtils;

import javax.jms.*;
import java.io.IOException;

/**
 * activemq消费者-主题
 */
public class TopicConsumer {

    private static final String DEFAULT_URL = "tcp://192.168.254.30:61616";

    private static final String TOPIC_NAME = "mercury_topic";

    public static void main(String[] args) throws JMSException, IOException {
        System.out.println("------2号消费者------");
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(DEFAULT_URL);
        Connection connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(TOPIC_NAME);
        MessageConsumer consumer = session.createConsumer(topic);
        consumer.setMessageListener((msg)->{
            if( msg != null && msg instanceof TextMessage){
                TextMessage textMessage = (TextMessage)msg;
                try {
                    System.out.println(textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
        System.in.read();

        consumer.close();
        session.close();
        connection.close();

    }
}